﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
}).use(['element', 'jquery', 'layer', 'form','table','layedit', 'md5', 'common'], function () {
    var element = layui.element,
        $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        layedit = layui.layedit,
        md5 = layui.md5,
        common = layui.common;
    var jhy = layui.data("jhy");

    //验证登录信息

    //判断是否有本地存储的用户信息
    if (jhy.user_id != undefined && jhy.user_account != undefined && jhy.user_password != undefined && jhy.user_name != undefined && jhy.user_role != undefined) {
        //ajax 验证账号和密码
        common.userLogin(jhy.user_account, jhy.user_password);
        $("#info-nav>li:first>a:first").text(jhy.user_name);
        element.render('nav', 'info-nav');       
    } else {
        //没有这些信息则清空表。
        layui.data("jhy", null);
        layui.data("url", {
            key: 'url',
            value: window.location.href
        })
        window.location.href = "/admin/login.html";
    }

    //加载用户菜单
    $.ajax({
        type: "post",
        url: "/Ashx/admin.ashx",
        data: { functionname: "jhy_user_menu", user_id: jhy.user_id },
        success: function (msg) {
            if (msg != "none") {
                $("#jhy-nav").html(msg);
                element.render("nav", "jhy-nav");
            }
        }
    });


    //顶部导航操作
    element.on('nav(info-nav)', function (elem) {
        var text = elem["context"].text;
        if (text == "退出") {
            layui.data("jhy", null);
            window.location.href = "/admin/login.html";
        } else if (text == "修改密码") {
            layer.prompt({
                formType: 1,
                title: '输入新密码'
            }, function (value, index, elem) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_user_edit_password", user_id: jhy.user_id, user_password: md5(value) },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.close(index);
                            layer.msg('修改成功，需要重新登录！', {
                                icon: 1
                            }, function () {
                                layui.data("jhy", null);
                                window.location.href = "/admin/login.html";
                            });
                        } else {
                            layer.msg("修改失败，请重新登录后重试！", { icon: 5 });
                        }
                    }
                });
                });
        } else if (text == "深度刷新") {
            if ($("#jhy-tab .layui-tab-content .layui-tab-item.layui-show").find("iframe").length > 0) {
                $("#jhy-tab .layui-tab-content .layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload(true);
            } else {
                layer.msg("没有框架");
            }       

        }
    });




    //start 根据屏幕显示打开关闭菜单按钮

    //初始化时生成
    if ($(window).width() <= 750) {
        $(".layui-layout-admin .layui-header .layui-menu i").attr("class", "layui-icon layui-icon-spread-left");
    } else {
        $(".layui-layout-admin .layui-header .layui-menu i").attr("class", "layui-icon layui-icon-shrink-right");

    }

    //窗口大小变化时生成
    $(window).resize(function () {
        var n = $(this).width();
        if (n <= 750) {
            $(".layui-layout-admin .layui-header .layui-menu i").attr("class", "layui-icon layui-icon-spread-left");
            $(".layui-layout-admin .layui-side").css("left", "-200px");
            $(".layui-layout-admin .layui-body").css("left", "0px");
            $(".layui-layout-admin .layui-footer").css("left", "0px");
        } else {
            $(".layui-layout-admin .layui-header .layui-menu i").attr("class", "layui-icon layui-icon-shrink-right");
            $(".layui-layout-admin .layui-side").css("left", "0px");
            $(".layui-layout-admin .layui-body").css("left", "200px");
            $(".layui-layout-admin .layui-footer").css("left", "200px");
        }
    });

    //点击按钮触发事件
    $(".layui-layout-admin .layui-header .layui-menu i").click(function () {
        var i = $(this);
        if (i.attr("class") == 'layui-icon layui-icon-spread-left') {
            $(".layui-body").addClass("jhy-shade");
            i.attr("class", "layui-icon layui-icon-shrink-right");
            $(".layui-layout-admin .layui-side").animate({ left: '0px' });
            $(".layui-layout-admin .layui-body").animate({ left: '200px' });
            $(".layui-layout-admin .layui-footer").animate({ left: '200px' });
        } else if (i.attr("class") == 'layui-icon layui-icon-shrink-right') {
            $(".layui-body").removeClass("jhy-shade");
            i.attr("class", "layui-icon layui-icon-spread-left");
            $(".layui-layout-admin .layui-side").animate({ left: '-200px' });
            $(".layui-layout-admin .layui-body").animate({ left: '0px' });
            $(".layui-layout-admin .layui-footer").animate({ left: '0px' });
        }
    });


    $(".jhy-body-shade").click(function () {
        $(".layui-body").removeClass("jhy-shade");
        $(".layui-layout-admin .layui-header .layui-menu i").attr("class", "layui-icon layui-icon-spread-left");
        $(".layui-layout-admin .layui-side").animate({ left: '-200px' });
        $(".layui-layout-admin .layui-body").animate({ left: '0px' });
        $(".layui-layout-admin .layui-footer").animate({ left: '0px' });
    });

    //start 根据屏幕显示打开关闭菜单按钮


    //start左侧菜单点击事件
    element.on('nav(jhy-nav)', function (elem) {
        if (elem.attr("jhy-url") != undefined) {
            var title = elem.html();
            var url = elem.attr("jhy-url");
            var iframe = '<iframe src=' + url + ' frameborder="0"></iframe> ';
            var layid = $.trim(elem.text());
            var layidcount = $("div[lay-filter='jhy-tab']").find(".layui-tab-title").find("li[lay-id='" + layid + "']").length;
            if (layidcount == 0) {
                //没有二级菜单的打开iframe
                element.tabAdd('jhy-tab', {
                    title: title,
                    content: iframe, //支持传入html
                    id: layid
                });
            }
            //跳转到该页
            element.tabChange('jhy-tab', layid);
            //如果是小屏幕，关闭菜单
            if ($(window).width() <= 750) {
                $(".jhy-body-shade").click();
            }
        }
    });


    //end左侧菜单点击事件




});  
